<template>
    <div>
        <el-select v-model="model"
                   placeholder="请选择"
                   @change="changed3"
                   :disabled="disabled"
        >
            <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
            >
            </el-option>
        </el-select>
    </div>
</template>
<script>
    import {loadDropDown} from "../request";
    export default {
        name: 'XDLinkSelecter',
        props: ["value", "list", "disabled"],
        data() {
            return {
                // 下拉
                options: [],
                model: "",
                select: {
                    url: '/devicetype/getList',
                    method: 'POST',
                    mineId: false,
                    companyId: false,
                    label: 'deviceTypeName',
                    value: 'deviceTypeId',
                    data: {}
                },
            }
        },
        created() {
            this.init3()
        },
        methods: {
            init3() {
                this.selectFunc()
                this.update3()
            },
            update3() {
                this.model = this.value
            },
            changed3() {
                this.$emit("input", this.model)
            },
            // 查询
            selectFunc() {
                this.select = this.list
                let requestJson = this.select
                if(this.$store.state.xdLink) {
                    console.log(requestJson)
                    requestJson.data = this.$store.state.xdLink
                }
                if(requestJson.mineId) {
                    requestJson.data.mineId = this.$store.state.mineId
                }
                if(requestJson.companyId) {
                    requestJson.data.companyId = this.$store.state.companyId
                }
                if(this.val) {
                    requestJson.data[this.prop] = this.val
                }
                loadDropDown(requestJson).then(response=>{
                    let list = response.data.list
                    let a = list.map(item=>({
                        label: item[this.select.label],
                        value: item[this.select.value]
                    }))
                    this.options = a
                }).catch(error => {
                    console.log(error)
                })
            },
        },
        watch: {
            "value": function() {
                this.update3()
            },
            "prop": function() {
                console.log("changed")
                this.update3()
            },
            "val": function() {
                console.log("changed")
                this.update3()
            },
            "$store.state.xdLink": function () {
                this.model = ""
                this.selectFunc()
            }
        }
    }
</script>